package is.ru.honn.rupin.data;

import java.sql.ResultSet;
import java.sql.SQLException;

import is.ru.honn.rupin.domain.User;
import is.ru.honn.rupin.domain.Gender;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

/**
 * Created with IntelliJ IDEA.
 * User: gmatt
 * Date: 19.10.2012
 * Time: 21:13
 *
 * Maps the column values to the appropriate variables in a User instance
 */
public class UserRowMapper implements ParameterizedRowMapper<User>{
    /**
     * @param rs The result set to map (1 or more rows of Users)
     * @param rowNum The number of the row to map
     * @return The appropriate User instance
     * @throws SQLException
     */
    public User mapRow(ResultSet rs, int rowNum) throws SQLException {
        if(rs.wasNull())
            return null;
        User user = new User(rs.getString(2),
                             rs.getString(3),
                             rs.getString(4),
                             rs.getString(5),
                             rs.getString(6),
                             Gender.valueOf(rs.getString(7)));
        user.setId(rs.getInt(1));
        return user;
    }
}
